package anc.tankshooter.ui;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;

import anc.tankshooter.GameActivity;
import anc.tankshooter.controls.Checkbox;
import anc.tankshooter.ui.SceneManager.SceneType;

public class SettingScreen extends IScreen {

	private Sprite mSettingBackgound;
	private AnimatedSprite mOkButton;
	private AnimatedSprite mCancelButton;
	private Checkbox mSoundCheckbox;
	private Checkbox mMusicCheckbox;

	private SettingButtonListener mListenner;
	
	public SettingScreen(SettingButtonListener settingButtonListener){
		mListenner = settingButtonListener;
	}
	
	@Override
	protected void createScene() {
		
		mSettingBackgound = new Sprite(200, 100, ResourcesManager.getInstance().mSettingBackgoundTexture, mVertextManager);
		mSettingBackgound.setAlpha(0.95f);
		attachChild(mSettingBackgound);
		this.setBackgroundEnabled(false);
		
		mOkButton = new AnimatedSprite(240, 280, ResourcesManager.getInstance().mOkButton, mVertextManager)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
				{
					setCurrentTileIndex(1);
				}
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP)
				{
					GameActivity.mGlobalSetting.setMusicOn(!mMusicCheckbox.Value());
					GameActivity.mGlobalSetting.setSoundOn(!mSoundCheckbox.Value());
					
					if(!mMusicCheckbox.Value()) 
						ResourcesManager.getInstance().resumeMusic();
					else 
						ResourcesManager.getInstance().pauseMusic();
					
					setCurrentTileIndex(0);
					if(mListenner!=null)
						mListenner.onOkClicked();
				}				
				GameActivity.saveSetting(GameActivity.DATABASE_FOLDER);
				return super .onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mCancelButton = new AnimatedSprite(410, 280, ResourcesManager.getInstance().mCancelButton, mVertextManager)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
				{
					setCurrentTileIndex(1);
				}
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP)
				{
					
					setCurrentTileIndex(0);
					if(mListenner!=null)
						mListenner.onCancleClicked();
				}
				// TODO Auto-generated method stub
				return super .onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mSoundCheckbox = new Checkbox(400, 220,!GameActivity.mGlobalSetting.isSoundOn())
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) this.Check();
				return super .onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mMusicCheckbox = new Checkbox(400, 172,!GameActivity.mGlobalSetting.isMusicOn())
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) this.Check();
				return super .onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		this.registerTouchArea(mSoundCheckbox);
		this.attachChild(mSoundCheckbox);
		
		this.registerTouchArea(mMusicCheckbox);
		this.attachChild(mMusicCheckbox);
		
		registerTouchArea(mOkButton);
		attachChild(mOkButton);
		
		registerTouchArea(mCancelButton);
		attachChild(mCancelButton);
	}


	@Override
	public boolean onBackKeyPressed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdateScreen(float second) {
		// TODO Auto-generated method stub
		
	}

	public static interface SettingButtonListener{
		public void onOkClicked();
		public void onCancleClicked();
	}
}
